package com.example.recyclerview.base;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public abstract class BaseDialogFragment extends DialogFragment {

    public abstract @LayoutRes
    int getLayout();

    public abstract void onCreateView(View view);

    public abstract void onActivityCreated(View view, @Nullable Bundle savedInstanceState);

    private View view;
    private int height = WindowManager.LayoutParams.WRAP_CONTENT;
    private int width = WindowManager.LayoutParams.WRAP_CONTENT;
    private int gravity = Gravity.CENTER;
    //是否使用透明背景
    private boolean isTransparent = true;
    //是否全屏
    private boolean isFullScreen = false;
    //用来判断是否多次点击。防止多次执行
    private boolean isAnimation = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setTransparent();
        view = inflater.inflate(getLayout(), container);
        onCreateView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        resizeDialogFragment();
        onActivityCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View getView() {
        return view;
    }

    private void setTransparent() {
        //设置透明背景
        if (isTransparent || isFullScreen) {
            Dialog dialog = getDialog();
            if (dialog != null) {
                Window window = dialog.getWindow();
                if (window != null) {
                    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                }
            }
        }
    }

    /**
     * 关闭透明背景
     */
    public void setCloseTransparent() {
        isTransparent = false;
    }

    /**
     * 全屏
     */
    public void setFullScreen() {
        isFullScreen = true;
    }

    /**
     * 修改布局的大小
     */
    private void resizeDialogFragment() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                if (isFullScreen) {
                    window.getDecorView().setPadding(0, 0, 0, 0);
                    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                } else {
                    layoutParams.height = height;
                    layoutParams.width = width;
                    layoutParams.gravity = gravity;
                }
                window.setAttributes(layoutParams);
            }
        }
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getGravity() {
        return gravity;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    /**
     * 从下到上进入的动画
     */
    public void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0
                , Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0);
        slide.setDuration(400);
        slide.setFillEnabled(true);
        slide.setFillAfter(true);
        view.startAnimation(slide);
    }

    /**
     * 从上到下退出的动画
     */
    private void slideToDown(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        slide.setDuration(400);
        slide.setFillEnabled(true);
        slide.setFillAfter(true);
        view.startAnimation(slide);
        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isAnimation = false;
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 退出的动画
     */
    public void dialogFinish(View view) {
        if (isAnimation) {
            return;
        }
        isAnimation = true;
        slideToDown(view);
    }

}
